package com.smart.sso.server.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.smart.mvc.service.mybatis.impl.ServiceImpl;
import com.smart.sso.server.dao.LoginIPLimitDao;
import com.smart.sso.server.model.LoginIPLimit;
import com.smart.sso.server.service.LoginIPLimitService;

@Service("LoginIPLimitService")
public class LoginIPLimitServiceImpl extends ServiceImpl<LoginIPLimitDao, LoginIPLimit, Integer> implements LoginIPLimitService {

	@Autowired
	public void setDao(LoginIPLimitDao dao) {
		this.dao = dao;
	}
	
	public void deleteByUserIds(List<Integer> idList) {
		dao.deleteByUserIds(idList);
	}
	
	public List<LoginIPLimit> findByUserId(Integer userId) {
		return dao.findByUserId(userId);
	}

	public boolean isIpPermitted(Integer userId, String ip) {
		boolean isSetIpRange = false;
		boolean result = false;
		long ipLong = ipToLong(ip);
		List<LoginIPLimit> ipList = this.findByUserId(userId);
		for(int i = 0; i<ipList.size(); i++)
		{
			isSetIpRange = true;
			LoginIPLimit loginIpLimit = ipList.get(i);
			String startIp = loginIpLimit.getStartIp();
			String endIp = loginIpLimit.getEndIp();
			if(ipLong>=ipToLong(startIp) && ipLong<=ipToLong(endIp))
			{
				result = true;
			}
		}
		if(isSetIpRange)
		{
			return result;
		}
		else
			return true;
	}
	
	//string形式的ipv4地址转换为整形数字
    public static long ipToLong(String strIp) {
    	
    	//应对java获取本地localhost为0:0:0:0:0:0:0:1问题。
    	if(strIp.equalsIgnoreCase("0:0:0:0:0:0:0:1"))
    	{
    		strIp = "127.0.0.1";
    	}
    	
        long[] ip = new long[4];
        //先找到IP地址字符串中.的位置
        int position1 = strIp.indexOf(".");
        int position2 = strIp.indexOf(".", position1 + 1);
        int position3 = strIp.indexOf(".", position2 + 1);
        //将每个.之间的字符串转换成整型
        ip[0] = Long.parseLong(strIp.substring(0, position1));
        ip[1] = Long.parseLong(strIp.substring(position1+1, position2));
        ip[2] = Long.parseLong(strIp.substring(position2+1, position3));
        ip[3] = Long.parseLong(strIp.substring(position3+1));
        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] << 8) + ip[3];
    }
    
    public void save(Integer userId, String ipRanges)
    {
    	if (!ipRanges.isEmpty()) {
    		String[] strs = ipRanges.split(",");
    		for (int i = 0, length = strs.length; i < length; i++) {
    			String[] ips = strs[i].split("-");
    			LoginIPLimit loginIPLimit = new LoginIPLimit();
    			if(ips.length==0)
    				break;
    			else if(ips.length==1)
    			{
        			loginIPLimit.setUserId(userId);
        			loginIPLimit.setStartIp(ips[0]);
    				loginIPLimit.setEndIp(ips[0]);
    			}
    			else
    			{
        			loginIPLimit.setUserId(userId);
        			loginIPLimit.setStartIp(ips[0]);
    				loginIPLimit.setEndIp(ips[1]);
    			}
    			save(loginIPLimit);
    		}
    	}
    }
    
    @Transactional
	public void deleteById(List<Integer> idList) {
    	dao.deleteByUserIds(idList);
    }
    
    public void save(LoginIPLimit t) {
		super.save(t);
	}
}
